<template>
  <div class="login_content">
    <el-row>
      <el-col :span="12" :xs="0">
      </el-col>
      <el-col :span="12" :xs="24">

        <el-form class="login_form" :model="loginform" :rules="rules" ref="loginrule">
          <h1>Hello</h1>
          <h2>欢迎welcome！</h2>


          <el-form-item prop="username">
            <el-input :prefix-icon="User" v-model="loginform.username"></el-input>
          </el-form-item>

          <el-form-item prop="password">
            <el-input :prefix-icon="Lock" :show-password="true" v-model="loginform.password" type="password"></el-input>
          </el-form-item>

          <el-form-item>
            <el-button :loading="loading" class="login_btn" size="default" @click="login" type="primary">登录</el-button>
          </el-form-item>


        </el-form>

      </el-col>
    </el-row>
  </div>
</template>

<script setup>
import { onMounted, reactive, ref } from 'vue';
import { User, Lock } from '@element-plus/icons-vue'
import { useRouter } from 'vue-router'
import userstore from '../store/user'
import { ElNotification } from 'element-plus'
import { gettime } from '../utils/time'
let loginrule = ref()
let $router = useRouter()
let myuserstore = userstore()
let loginform = reactive({
  username: '',
  password: ''
})
const rulename = (rule, value, callback) => {
  if (value.length >= 5) {
    callback()
  } else {
    callback(new Error('账号至少五位！'))
  }
}
const rulepassword = (rule,value, callback) => {
  if (value.length >= 6) {
    callback()
  } else {
    callback(new Error('密码至少六位！'))
  }
}
const rules = {
  username: [
    { trigger: 'change', validator: rulename }
  ],
  password: [
    { trigger: 'change', validator: rulepassword }
  ]
}
let loading = ref(false)
const login = async () => {
  await loginrule.value.validate()
  loading.value = true
  try {
    await myuserstore.userlogin(loginform)
    $router.push('/')

    ElNotification({
      type: 'sucess',
      message: '登陆成功',
      title: `Hi,${gettime()}好`
    })
    loading.value = false
  } catch (error) {
    loading.value = false

    ElNotification({
      type: 'error',
      message: error.data.message
    })
  }
}
onMounted(() => {
  gettime()
})
</script>
<style lang="scss" scoped >
.login_content {
  width: 100%;
  height: 100vh;
  background: url('/src/assets/images/background.jpg') no-repeat;
  background-size: cover;

  .login_form {
    padding: 40px;
    position: relative;
    width: 80%;
    top: 30vh;
    background: url('/src/assets/images/login_form.png') no-repeat;
    background-size: cover;
    color: white;

    h1 {
      font-size: 40px;
    }

    h2 {
      font-size: 20px;
      margin: 20px 0;
    }

    .login_btn {
      width: 100%;
    }
  }
}
</style>